package cn.hou.stream;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.junit.Before;
import org.junit.Test;

import jdk.nashorn.internal.runtime.FindProperty;
public class TestTransaction {
	
	List<Transaction> transactions = null;
	@Before
	public void before(){
		//交易员    名字 城市
		Trader yaozhen = new Trader("姚振", "武汉");
		Trader heyi = new Trader("何毅", "深圳");
		Trader cuijie = new Trader("崔杰", "北京");
		Trader lishibiao = new Trader("李世彪", "武汉");
		transactions = Arrays.asList(
				//交易类    交易员  时间   金额
				new Transaction(lishibiao, 2011, 300),
				new Transaction(yaozhen, 2012, 1000),
				new Transaction(yaozhen, 2011, 400),
				new Transaction(heyi, 2012, 710),
				new Transaction(heyi, 2012, 700),
				new Transaction(cuijie, 2012, 950)
		);
	}
	@Test
	public void test01(){
		//1 找出2011年发生的所有交易,并按交易额排序(从低到高)
		transactions.stream()
				.filter(x->x.getYear()==2011)
				.sorted((x,y)->Integer.compare(x.getValue(), y.getValue()))
				.forEach(System.out::println);
		//2 交易员都在哪些不同的城市工作过
		transactions.stream().map(x->x.getTrader().getCity())
				.distinct().forEach(System.out::println);
		//3查找所有来自武汉的交易员,并按姓名排序
		transactions.stream().filter(x->x.getTrader().getCity().equals("武汉"))
				.map(Transaction::getTrader).distinct()
				.sorted((x,y)->x.getName()
				.compareTo(y.getName())).forEach(System.out::println);
	}
	@Test
	public void test02(){
		//4返回所有交易员的姓名字符串,按字母排序
		String reduce = transactions.stream().map(x->x.getTrader().getName())
				.distinct().sorted().reduce("", String::concat);
		System.out.println(reduce);
		//5有没有交易员是在上海工作过的
		boolean anyMatch = transactions.stream()
				.anyMatch(x->x.getTrader().getCity().equals("上海"));
		System.out.println(anyMatch);
		//6打印生活在武汉的 交易员的所有交易额
		transactions.stream().filter(x->x.getTrader().getCity().equals("武汉"))
				.map(x->x.getValue()).forEach(System.out::println);
		//7找到最高的交易额
		Optional<Integer> max = transactions.stream()
				.map(x->x.getValue()).distinct().max(Integer::compareTo);
		System.out.println(max.get());
		//8找到交易额最小的交易  排序后取第一个即最小的
		Optional<Transaction> findFirst = transactions.stream()
				.sorted((x,y)->Integer.compare(x.getValue(), x.getValue())).findFirst();
		System.out.println(findFirst.get());
		//直接取最小
		Optional<Transaction> min = transactions.stream()
				.min((x,y)->Integer.compare(x.getValue(), y.getValue()));
		System.out.println(min.get());
	}
	
}